package GDoc::Job::Fetch::US::Exec::HHS::ATSDR::MMGs;

use Moose;
use WWW::Mechanize;
use GDoc::Job;

extends 'GDoc::Job::Fetch';

sub setup {
    my $self = shift;
    $self->_baseurl( 'http://www.atsdr.cdc.gov/MHMI/mmg.html' );
    $self->_basedir( 'http://www.atsdr.cdc.gov/MHMI/' );
}

sub run { 
    my $self = shift;

    if ( $self->data->{link} ) { 
        $self->_fetch_pdf;
    } else { 
        $self->logger->info( "fetching base " . $self->_baseurl );
        my $mech = $self->_mech;
        $mech->get( $self->_baseurl );

        my %seen;
        my @links = grep { !$seen{$_->url}++ } $mech->find_all_links( url_regex => qr{^mmg\d+\.html} );

        foreach my $link( @links ) { 
            my $url = $self->_basedir . $link->url;
            $url =~ /mmg(\d+)\.html/;
            my $num = $1;

            my $job = GDoc::Job->new( class => 'Fetch::US::Exec::HHS::ATSDR::MMGs',
                                      queue => $self->fetch_queue,
                                      data  => { link => $url,
                                                 desc => $link->text,
                                                 num  => $num }
                                    );

            $job->submit;
        }
    }
}

sub _fetch_pdf { 
    my $self = shift;
    my %data = %{ $self->data };

    my $mech = $self->_mech;

    $self->logger->info( "Fetching index page $data{link}" );
    $mech->get( $data{link} );

    my $num = $data{num};
    my $pdf_link = $mech->find_link( url_regex => qr{mmg${num}\.pdf$} );
    
    $self->logger->info( "Found PDF link " . $pdf_link->url );

    $mech->get( $pdf_link );

    my $prefix = 'ATSDR-MMG';

    my $key = sprintf( 'us/exec/hhs/atsdr/%s-%04d.pdf', $prefix, $data{num} );
    
    $self->logger->info( 'storing ' . $pdf_link->url . " --> $key" );

    $self->store_pdf( key       => $key,
                      data      => $mech->response->content,
                      filename  => sprintf( '%s-%04d.pdf', $prefix, $data{num} ),
                      meta      => { description   => $data{desc},
                                     identifier    => sprintf( '%s-%04d', $prefix, $data{num} )
                                   }
                    );

}


1;
